import {echatIMSDK, EChatIMSDKConfig} from "./libs/sdk/echatim/core/sdk/EChatIMSDK";
import {globalStore} from "./libs/ShareStore";
import Logger from "./libs/sdk/echatim/log/Logger";
import {DeviceEventEmitter, Platform} from "react-native";
import {ScreenDimensions} from "./libs/utils/ScreenDimensions";
import Beans from "./libs/sdk/echatim/common/utils";
import {BroadcastEvent} from "./events/BroadcastEvent";
import {DevConfig} from "./dev";


export default class AppInitializer {
    init(auid:string, token:string){
        console.log(`==> screen size:${Beans.json(ScreenDimensions.screen)}, 1dp=${ScreenDimensions.pxperdp}px`);

        let sdkConfig = new EChatIMSDKConfig();
        sdkConfig.host = DevConfig.HOST;
        sdkConfig.httpPort = DevConfig.HTTP_PORT;
        sdkConfig.socketPort = DevConfig.SOCKET_PORT;
        sdkConfig.key = 'TSDKTEST00001';
        sdkConfig.secret = '';
        sdkConfig.apiTransport = 'HTTP';
        // sdkConfig.loginAuid = 'admin';
        // sdkConfig.loginToken = 'admin';
        sdkConfig.loginAuid = auid;
        sdkConfig.loginToken = token;
        sdkConfig.listeners = globalStore.sdkListener;
        sdkConfig.fileServerConfig = {
            // use: 'aliyun',
            use: 'local',
            // client: (Platform.OS === 'android' || Platform.OS === 'ios') ? 'rn-fetch' : 'plupload',
            client: (Platform.OS === 'android' || Platform.OS === 'ios') ? 'rn-xmlhttp' : 'plupload',
            // client: (Platform.OS === 'android' || Platform.OS === 'ios') ? 'rn-xmlhttp' : 'xmlhttp',
            baseUrl: DevConfig.FILE_SERVER_URL,
            version: 'v1',
        };
        // app.userInfo.auid = sdkConfig.loginAuid;
        globalStore.userInfo.auid = sdkConfig.loginAuid;
        echatIMSDK.init(sdkConfig, function (sdk) {
            if(sdk){
                Logger.info('echatIMSDK 成功连接, 可以使用 EChatIMApis 请求数据了.');
                DeviceEventEmitter.emit(BroadcastEvent.EVENT_SDK_INIT, sdk)
            }
        });
    }
}

export const appInitializer = new AppInitializer();
